package cn.sciento.dd.service.impl;

import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import cn.sciento.dd.dto.CreateDeptResultDTO;
import cn.sciento.dd.dto.DefaultResultDTO;
import cn.sciento.dd.dto.DeptListDTO;
import cn.sciento.dd.dto.DingSyncDeptDTO;
import cn.sciento.dd.dto.DingSyncUserDTO;
import cn.sciento.dd.dto.GetAdminListResultDTO;
import cn.sciento.dd.dto.GetAdminScopeResultDTO;
import cn.sciento.dd.dto.GetDeptDTO;
import cn.sciento.dd.dto.GetDeptUserInfoResultDTO;
import cn.sciento.dd.dto.GetDeptUserResultDTO;
import cn.sciento.dd.dto.GetInactiveUserDTO;
import cn.sciento.dd.dto.GetInactiveUserResultDTO;
import cn.sciento.dd.dto.GetListParentDeptsByDeptIdResultDTO;
import cn.sciento.dd.dto.GetListParentDeptsByUserId;
import cn.sciento.dd.dto.GetOrgUserCountResultDTO;
import cn.sciento.dd.dto.GetSubDeptDTO;
import cn.sciento.dd.dto.GetUserDTO;
import cn.sciento.dd.dto.GetUserInfoByCodeResultDTO;
import cn.sciento.dd.dto.GetUserListDTO;
import cn.sciento.dd.dto.GetUseridByMobileResultDTO;
import cn.sciento.dd.dto.GetUseridByUnionidResultDTO;
import cn.sciento.dd.dto.UpdateDeptResultDTO;
import cn.sciento.dd.dto.UserCreateResultDTO;
import cn.sciento.dd.service.DingCorpAddressService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class DingCorpAddressServiceImpl implements DingCorpAddressService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public UserCreateResultDTO createUser(String accessToken, DingSyncUserDTO userDTO) {
    return (UserCreateResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/user/create?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(userDTO), (MultiValueMap)buildHttpHeaders()), UserCreateResultDTO.class);
  }
  
  public DefaultResultDTO updateUser(String accessToken, DingSyncUserDTO userDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/user/update?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(userDTO), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO deleteUserById(String accessToken, String userid) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/delete?access_token=" + accessToken + "&userid=" + userid, DefaultResultDTO.class);
  }
  
  public GetUserDTO getUserInfoByUserId(String accessToken, String userid, String lang) {
    String url = "https://oapi.dingtalk.com/user/get?access_token=" + accessToken + "&userid=" + userid;
    if (!StringUtils.isBlank(lang))
      url = url + "&lang=" + lang; 
    return (GetUserDTO)this.restTemplate.getForObject(url, GetUserDTO.class);
  }
  
  public GetUserListDTO getUserListByDeptId(String accessToken, String deptId) {
    return (GetUserListDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/getDeptMember?access_token=" + accessToken + "&deptId=" + deptId, GetUserListDTO.class);
  }
  
  public GetDeptUserInfoResultDTO getDeptUserInfo(String accessToken, String lang, Long department_id, Long offset, Long size, String order) {
    return (GetDeptUserInfoResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/listbypage?access_token=" + accessToken + "&lang=" + lang + "&department_id" + department_id + "&offset" + offset + "&size" + size + "&order" + order, GetDeptUserInfoResultDTO.class);
  }
  
  public GetDeptUserResultDTO getDeptUser(String accessToken, String lang, Long department_id, Long offset, Long size, String order) {
    return (GetDeptUserResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/simplelist?access_token=" + accessToken + "&lang=" + lang + "&department_id" + department_id + "&offset" + offset + "&size" + size + "&order" + order, GetDeptUserResultDTO.class);
  }
  
  public GetAdminListResultDTO getAdminList(String accessToken) {
    return (GetAdminListResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/get_admin?access_token=" + accessToken, GetAdminListResultDTO.class);
  }
  
  public GetAdminScopeResultDTO getAdminScope(String accessToken, String userid) {
    return (GetAdminScopeResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/topapi/user/get_admin_scope?access_token=" + accessToken + "&userid=" + userid, GetAdminScopeResultDTO.class);
  }
  
  public GetUseridByUnionidResultDTO getUseridByUnionid(String accessToken, String unionid) {
    return (GetUseridByUnionidResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/getUseridByUnionid?access_token=" + accessToken + "&unionid=" + unionid, GetUseridByUnionidResultDTO.class);
  }
  
  public GetUseridByMobileResultDTO getUseridByMobile(String accessToken, String mobile) {
    return (GetUseridByMobileResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/get_by_mobile?access_token=" + accessToken + "&mobile=" + mobile, GetUseridByMobileResultDTO.class);
  }
  
  public GetOrgUserCountResultDTO getOrgUserCount(String accessToken, Long onlyActive) {
    return (GetOrgUserCountResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/user/get_org_user_count?access_token=" + accessToken + "&onlyActive=" + onlyActive, GetOrgUserCountResultDTO.class);
  }
  
  public GetInactiveUserResultDTO getInactiveUser(String accessToken, GetInactiveUserDTO getInactiveUserDTO) {
    return (GetInactiveUserResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/topapi/inactive/user/get?access_token=" + accessToken, new HttpEntity(getInactiveUserDTO, (MultiValueMap)buildHttpHeaders()), GetInactiveUserResultDTO.class);
  }
  
  public GetUserInfoByCodeResultDTO getUserInfoByCode(String accessToken, String code) {
    return (GetUserInfoByCodeResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + accessToken + "&code" + code, GetUserInfoByCodeResultDTO.class);
  }
  
  public CreateDeptResultDTO createDept(String accessToken, DingSyncDeptDTO deptDTO) {
    return (CreateDeptResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/department/create?access_token=" + accessToken, new HttpEntity(deptDTO, (MultiValueMap)buildHttpHeaders()), CreateDeptResultDTO.class);
  }
  
  public UpdateDeptResultDTO updateDept(String accessToken, DingSyncDeptDTO deptDTO) {
    return (UpdateDeptResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/department/update?access_token=" + accessToken, new HttpEntity(deptDTO, (MultiValueMap)buildHttpHeaders()), UpdateDeptResultDTO.class);
  }
  
  public DefaultResultDTO deleteDeptById(String accessToken, String id) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/delete?access_token=" + accessToken + "&id=" + id, DefaultResultDTO.class);
  }
  
  public DeptListDTO getDeptList(Long id, String accessToken, String lang, String fetch_child) {
    return (DeptListDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/list?access_token=" + accessToken + "&id=" + id + "&lang=" + lang + "&fetch_child=" + fetch_child, DeptListDTO.class);
  }
  
  public GetDeptDTO getDeptById(String accessToken, String id, String lang) {
    return (GetDeptDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/get?access_token=" + accessToken + "&id=" + id + "&lang=" + lang, GetDeptDTO.class);
  }
  
  public GetSubDeptDTO getSubDeptbyId(String accessToken, String id) {
    return (GetSubDeptDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/list_ids?access_token=" + accessToken + "&id=" + id, GetSubDeptDTO.class);
  }
  
  public GetListParentDeptsByDeptIdResultDTO getListParentDeptsByDeptId(String accessToken, String id) {
    return (GetListParentDeptsByDeptIdResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/list_parent_depts_by_dept?access_token=" + accessToken + "&id=" + id, GetListParentDeptsByDeptIdResultDTO.class);
  }
  
  public GetListParentDeptsByUserId getListParentDeptsByUserId(String accessToken, String userId) {
    return (GetListParentDeptsByUserId)this.restTemplate.getForObject("https://oapi.dingtalk.com/department/list_parent_depts?access_token=" + accessToken + "&userId=" + userId, GetListParentDeptsByUserId.class);
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
